﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HPIT.RentHouse.DTO;
using RentHouse.Common;
using RentHouse.IService;
using RentHouse.Service.Entities;

namespace RentHouse.Service
{
    public class IdNameService : IIdNameService
    {
        public static List<IdNameDTO> idNameDTOList;   //创建静态的泛型集合存储T_IdNames表里的信息，这样每次就不用再次从数据库中读
        public IdNameService()    //实例化时自动查询T_IdNames表里的所有信息并存到泛型集合里面
        {
            using (var db = new RentHouseContext())
            {
                var bs = new BaseService<T_IdNames>(db);
                idNameDTOList = bs.GetList(e=>true).Select(e=>new IdNameDTO() { 
                Id=e.Id,
                Name=e.Name,
                TypeName=e.TypeName
                }).ToList();

            }
        }

        /// <summary>
        /// 根据TypeName查对应的名称类型,利用静态成员中缓存的数据查找
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<IdNameDTO> GetIdNameList(IdNameEnum typeName)
        {
            return idNameDTOList.Where(e => e.TypeName == typeName.ToString()).ToList();


        }

        /// <summary>
        /// 通过id在关联表中查询名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetIdsName(long id)
        {
            return idNameDTOList.Where(e => e.Id == id).FirstOrDefault().Name;
        }
    }
}
